package com.newmainsoftech.logging.wrapper;


/**
 * When sub-class does not have static method for logging, then extend from this, and obtain 
 * LoggerWrapContract instance for logging via getLoggerWrapper method.<br />
 * For the class with static method, use getLoggerWrapper method and getLoggerWrapperFunctions method 
 * of LoggerWrapFactory class instead.
 * @author ay
 */
public abstract class LoggerWrapAdapterTemplate {
	// For logging --------------------------------------------------------------------------------
	abstract protected String getNameForLogging();
	
	protected final Object loggerWrapperLock = new Object();
	
	private LoggerWrapFactoryContract loggerWrapperFactory;
		/**
		 * @return LoggerWrapFactoryContract object held by loggerWrapperFactory member field. <br />
		 * If loggerWrapperFactory member field value is null when this method is called, then 
		 * LoggerWrapFactory.FacoryInstance will be set to loggerWrapperFactory member field. 
		 */
		protected LoggerWrapFactoryContract getLoggerWrapperFactory() {
			synchronized( loggerWrapperLock) {
				return loggerWrapperFactory;
			} // synchronized
		}
		protected void setLoggerWrapperFactory( LoggerWrapFactoryContract loggerWrapperFactory) {
			synchronized( loggerWrapperLock) {
				this.loggerWrapperFactory = loggerWrapperFactory;
				setLoggerWrapper( null);
			} // synchronized
		}
	
	private LoggerWrapContract loggerWrapper;
		/**
		 * @return loggerWrapper member field. <br />
		 * If loggerWrapper member field value is null when this method is called, then it will be 
		 * obtained via getLoggerWrapper method of loggerWrapperFactory member field. At that time, if 
		 * loggerWrapperFactory member field value is null, then LoggerWrapFactory.FacoryInstance 
		 * will be set to loggerWrapperFactory member field. 
		 */
		protected LoggerWrapContract getLoggerWrapper() {
			synchronized( loggerWrapperLock) {
				if ( loggerWrapper == null) {
					setLoggerWrapper( 
							LoggerWrapFactory.getLoggerWrapper( 
									getNameForLogging(), 
									getLoggerWrapperFactory()
									)
							);
				}
				return loggerWrapper;
			} // synchronized
		}
		protected void setLoggerWrapper( LoggerWrapContract loggerWrapper) {
			synchronized( loggerWrapperLock) {
				this.loggerWrapper = loggerWrapper;
			} // synchronized
		}
	// --------------------------------------------------------------------------------------------
}
